﻿@using QH.JobBack.Model;
@{
    var moduleList = ViewBag.moduleList as List<Module>;
    var moduleRole = ViewBag.moduleRole as ModuleRole;
    var moduleMenu = ViewBag.moduleMenu as List<MenuInfo>;
    var moduleFunc = ViewBag.moduleFunc as List<Function>;
    var roleFunction = ViewBag.roleFunction as List<RoleFunctionPermission>;
    var role = Model as RoleInfo;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link href="~/role_JsAndCss/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/role_JsAndCss/css/style.css" />
    <link href="~/role_JsAndCss/assets/css/codemirror.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/role_JsAndCss/assets/css/ace.min.css" />
    <link rel="stylesheet" href="~/role_JsAndCss/font/css/font-awesome.min.css" />
    <!--[if lte IE 8]>
      <link rel="stylesheet" href="~/assets/css/ace-ie.min.css" />
    <![endif]-->
    <script src="~/role_JsAndCss/js/jquery-1.9.1.min.js"></script>
    <script src="~/role_JsAndCss/assets/js/bootstrap.min.js"></script>
    <script src="~/role_JsAndCss/assets/js/typeahead-bs2.min.js"></script>
    <script src="~/role_JsAndCss/assets/js/jquery.dataTables.min.js"></script>
    <script src="~/role_JsAndCss/assets/js/jquery.dataTables.bootstrap.js"></script>
    <script src="~/role_JsAndCss/assets/layer/layer.js" type="text/javascript"></script>
    <script src="~/role_JsAndCss/assets/laydate/laydate.js" type="text/javascript"></script>
    <script src="~/role_JsAndCss/js/dragDivResize.js" type="text/javascript"></script>
    <title>添加权限</title>
</head>

<body>
    <div class="Competence_add_style clearfix">
        <div class="left_Competence_add">
            <div class="title_name">添加权限</div>
            <div class="Competence_add">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 所属组 </label>
                    <div class="col-sm-9">
                        <select name="ModuleId" id="ModuleId">
                            <option value="">请选择</option>
                            @foreach (var item in moduleList)
                            {
                                if (item.Id == @moduleRole.ModuleId)
                                {
                                    <option value="@item.Id" selected=selected>@item.Name</option>

                                }
                                else
                                {
                                    <option value="@item.Id">@item.Name</option>

                                }
                            }
                        </select>
                    </div>

                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 角色名称 </label>
                    <div class="col-sm-9"><input type="text" value="@role.RoleName" id="RoleName" placeholder="" name="角色名称" class="col-xs-10 col-sm-5" /></div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 角色描述 </label>
                    <div class="col-sm-9"><textarea name="角色描述" class="form-control" id="Remark" placeholder="" onkeyup="checkLength(this);">@role.Remark</textarea><span class="wordage">剩余字数：<span id="sy" style="color:Red;">200</span>字</span></div>
                </div>

                <!--按钮操作-->
                <div class="Button_operation">
                    <button onclick="Submit();" class="btn btn-primary radius" type="submit"><i class="fa fa-save "></i> 保存并提交</button>
                    <button onclick="article_save();" class="btn btn-secondary  btn-warning" type="button"><i class="fa fa-reply"></i> 返回上一步</button>
                    <button onclick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </div>
        <!--权限分配-->
        <div class="Assign_style">
            <div class="title_name">权限分配</div>
            <div class="Select_Competence">
                @foreach (var parent in moduleMenu.Where(a => a.ParentId == 1))
                {
                    <dl class="permission-list">
                        <dt><label class="middle"><input name="parent" class="ace" type="checkbox" id="parent" /><span class="lbl">@parent.MenuName</span></label></dt>
                        <dd>
                            @foreach (var child in moduleMenu.Where(a => a.ParentId != 0 && a.ParentId == parent.MenuId))
                            {
                                <dl class="cl permission-list2">
                                    <dt><label class="middle"><input type="checkbox"  @(roleFunction.Exists(a => a.MenuId==child.MenuId) ? "checked":"") value="" class="ace" name="child" id="child" /><span class="lbl">@child.MenuName</span></label></dt>
                                    <dd>
                                        @foreach (var func in moduleFunc.Where(a => a.MenuId == child.MenuId))
                                        {
                                            <label class="middle"><input type="checkbox" value="" @(roleFunction.Exists(a => a.FunctionId==func.Id) ? "checked":"") functionid="@func.Id" menuid="@func.MenuId" class="func" name="func" id="func" /><span class="lbl">@func.NAME</span></label>
                                        }

                                    </dd>
                                </dl><br />
                            }
                        </dd>
                    </dl>
                }


            </div>
        </div>
    </div>
    <input type="hidden" value="@role.RoleId" id="RoleId" />
</body>
</html>
<script type="text/javascript">
    //初始化宽度、高度
    $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();;
    $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
    $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
    //当文档窗口发生改变时 触发
    $(window).resize(function () {

        $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
        $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
        $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();;
    });
    /*字数限制*/
    function checkLength(which) {
        var maxChars = 200; //
        if (which.value.length > maxChars) {
            layer.open({
                icon: 2,
                title: '提示框',
                content: '您出入的字数超多限制!',
            });
            // 超过限制的字数了就将 文本框中的内容按规定的字数 截取
            which.value = which.value.substring(0, maxChars);
            return false;
        } else {
            var curr = maxChars - which.value.length; //250 减去 当前输入的
            document.getElementById("sy").innerHTML = curr.toString();
            return true;
        }
    };
    /*按钮选择*/
    $(function () {
        $(".permission-list dt input:checkbox").click(function () {
            $(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
        });
        $(".permission-list2 dd input:checkbox").click(function () {
            var l = $(this).parent().parent().find("input:checked").length;
            var l2 = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if ($(this).prop("checked")) {
                $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
            }
            else {
                if (l == 0) {
                    $(this).closest("dl").find("dt input:checkbox").prop("checked", false);
                }
                if (l2 == 0) {
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                }
            }

        });
    });

    //获取数据
    function Submit() {
        var data = {};
        data.ModuleId = $("#ModuleId").val();//获取组Id
        data.RoleName = $("#RoleName").val();//获取角色名称
        data.Remark = $("#Remark").val();//描述
        data.RoleId = $("#RoleId").val();//角色Id

        var funcs = [];

        $("#func:checked").each(function () {
            var func = {};
            var _this = this;
            func.RoleId = data.RoleId;
            func.MenuId = $(this).attr("menuid");
            func.FunctionId = $(this).attr("functionid");
            funcs.push(func);
        })

        data.RoleFunction = funcs;
        //提交
        $.ajax({
            url: "/Role/UpdateRoleInfo?jsondata="+JSON.stringify(data),
            method: 'post',
            dataType: 'JSON',
            success: function (data) {
                if (data.success) {
                    //验证码效验成功之后提交到后台
                    layer.msg(data.msg, {
                        title: '提示框',
                        icon: 1,
                        time: 3000,
                        end: function () {
                            window.location.href = "/Role/Index";
                        }
                    });
                }
                else {
                    layer.alert(data.msg);
                    return false;
                }
            },
            error: function (data) {

            }
        });
    }
</script>
